//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Packaging;
using System.Text;
using System.Xml;
using OpenLS.Core;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Drawing
{
    public class ThemeDocument : OfficeDocument
    {
        private const string documentRelationship =
            @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";

        private readonly List<Theme> _themes = new List<Theme>();

        protected override string DefaultName
        {
            get { return "Theme"; }
        }

        public List<Theme> Themes
        {
            get { return _themes; }
        }

        public static ThemeDocument Open(string fileName)
        {
            ThemeDocument result = new ThemeDocument();
            using (Package p = Package.Open(fileName, FileMode.Open, FileAccess.Read))
            {
                result.ReadPackage(p);
            }
            return result;
        }

        public override void ReadPackage(Package package, BackgroundWorker opContext)
        {
            PackageRelationship rel = null;
            foreach (PackageRelationship relationship in package.GetRelationships())
            {
                if (relationship.RelationshipType == documentRelationship)
                    rel = relationship;
            }
            if (rel == null)
                throw new NotSupportedException();
            PackagePart documentPart = package.GetPart(PackUriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri));
            ThemeManager man;
            using (Stream stream = documentPart.GetStream())
            using (XmlTextReader xr = new XmlTextReader(stream))
            {
                ReadContext ctx = new ReadContext(xr, documentPart);
                man = ctx.ReadElement<ThemeManager>();
            }
            Themes.Clear();
            foreach (Theme th in man.Themes)
                Themes.Add(th);
        }

        protected override void SetNotDirty()
        {
            
        }

        public override void WritePackage(Package package)
        {
            Uri themeManagerUri = new Uri("/themeManager.xml", UriKind.Relative);
            PackageRelationship r =
                package.CreateRelationship(themeManagerUri, TargetMode.Internal, documentRelationship);
            Uri uu = PackUriHelper.ResolvePartUri(r.SourceUri, r.TargetUri);
            PackagePart themeManagerPart =
                package.CreatePart(uu, DrawingConstants.ThemeContentType, CompressionOption.Maximum);
            ThemeManager m = new ThemeManager();
            m.Themes = Themes;

            using (Stream stream = themeManagerPart.GetStream())
            using (XmlWriter xmlWriter = new XmlTextWriter(stream, Encoding.UTF8))
            {
                WriteContext c = new WriteContext(DrawingConstants.DrawingNamespace, xmlWriter, themeManagerPart);
                c.WriteElement(m);
            }
        }

    }
}